<?php
            
use app\model\Comm; 
use app\model\Theme; 
use app\model\Poster; 
use app\model\Worker; 
use app\model\Userdata; 


/**
 * 业务逻辑封装
 * 使用tablename()函数不要忘记引入define.php,同时需要在define.php中需要定义好表别名
 */
require_once PATH_MODULE .'define.php';
class WeikeModel extends PluginModel
{

    /**
     * @param $themeid
     * @param int $type
     * @param int $page_start
     * @param int $page_size
     * @return mixed
     */
    public static function getWeikeList($themeid, $search = '', $page_start = 0, $page_size = 10, $scan = 0, $workerid = 0)
    {
        $condition = "";
        if($workerid > 0){
            $condition .= " AND a.config->'$.workerid' = '$workerid' ";
        }
        //1 总数 2 已绑定 3 已兑换 4 未绑定 5未兑换 6 进行中
        if($scan == 2){
            $condition .= ' AND a.uid>0 ';
        }
        if($scan == 3){
            $condition .= ' AND a.has_case_gift=1 ';
        }
        if($scan == 4){
            $condition .= ' AND a.uid=0 ';
        }
        if($scan == 5){
            $condition .= ' AND a.has_case_gift=0 ';
        }
        if($scan == 6){
            $today = date('Y-m-d');
            $condition .= " AND a.config->'$.end_time' >= '$today' ";
        }
        if ($search){
            $condition .= " AND a.name LIKE '" . $search . "'" ;
        }
        if($page_size == 0){
            $sql = " select count(a.id) as count_num from "
                . tablename(wk_info) . " as a " .
                "left join ". tablename(t_fans) . " as b on a.uid=b.uid AND b.themeid='$themeid' " .
                " where a.themeid='$themeid' AND a.delete = 0 " . $condition;
            return db_find($sql);
        }else{
            $condition .= " order by a.created_at desc ";
            $condition .= "  limit " . $page_start . "," . $page_size;

            $sql = " select a.*,b.avatar,b.nickname,b.mobile,b.username from "
                . tablename(wk_info) . " as a " .
                "left join ". tablename(t_fans) . " as b on a.uid=b.uid AND b.themeid='$themeid' " .
                " where a.themeid='$themeid'  AND a.delete = 0 " . $condition;
//            var_dump($sql);die;
            return db_select($sql);
        }
    }

    public static function getWeikeCountByWhere($themeid,$where = '')
    {
        $sql = " select count(a.id) as count_num from "
            . tablename(wk_info) . " as a " .
            " where a.themeid='$themeid' AND a.delete = 0 " . $where;
        $res = db_find($sql);
        return $res['count_num']*1;
    }

    /**
     * Desc:
     * User:wp
     * Email:1565602678@qq.com
     * Time: 2022/5/20 4:50 下午
     * @param $id
     * @param $themeid
     * @return mixed
     */
    public static function getWkInfoById($id,$themeid){
        $result = db_find("select * from ". tablename(wk_info) ." where id='$id' and themeid='$themeid'");
        if(!empty($result)){
            $result['config'] = json_decode($result['config'],true);
        }
        return $result;
    }

    public static function getShareinfo($fans,$wkInfo){
        global $_C,$_G,$_P;
        $config = $_C;
        if ($_G['member']['uid']==$wkInfo['uid']){
            $sharetitle = $config['sharetitlexcx2'];
        }else{
            $sharetitle = $config['sharetitlexcx'];
        }
        if (empty($sharetitle)){
            $sharetitle = "USERNAME的预告片新鲜上映了！SHOPNAME";
        }

        $sharetitle = str_replace("FANSNICK",$fans['nickname'],$sharetitle);
        $sharetitle = str_replace("FANSNAME",$fans['username'],$sharetitle);
        $sharetitle = str_replace("USERNAME",$wkInfo['name'],$sharetitle);
        $sharetitle = str_replace("SHOPNAME",$config['jubf'],$sharetitle);
        if ($wkInfo['config']['workerid'] && $config['isshowkefu']==1) {
            $theyworker = Worker::getThemeWorkerByUid($wkInfo['config']['workerid'], $wkInfo['themeid'], 1);
            if (strpos($sharetitle, "WORKER") !== false){
                $sharetitle = str_replace("WORKER",$theyworker['username'],$sharetitle);
            }elseif (!empty($theyworker['username'])){
                $sharetitle = $sharetitle ."【客服:".$theyworker['username']."】";
            }
        }
        if (!empty($wkInfo['config']['shareimg'])){
            $shareimgurl = tomedia($wkInfo['config']['shareimg']);
        }else{
            $shareimgurl = tomedia($wkInfo['config']['photo'][0]);
        }
        $sharedata = array(
            'fromuid'=>$_G['member']['uid'],
            'wkuid'=>$wkInfo['uid'],
            'wk_id'=>$wkInfo['id'],
            'themeid'=>$wkInfo['themeid'],
            'path'=>Userdata::MyPath($wkInfo['themeid'],$wkInfo['config']['workerid'],$fans['uid'],$_P['path']),
        );
        $shareurl =  Theme::createwxAppUrl('weike/pages/weike/weike',$sharedata);

        $shareinfo['sharetitle'] = $sharetitle;
        $shareinfo['shareimgurl'] = $shareimgurl;
        $shareinfo['shareurl'] = $shareurl;
        return $shareinfo;
    }

    /**
     * Desc:
     * User:wp
     * Email:1565602678@qq.com
     * Time: 2022/5/20 4:50 下午
     * @param $data
     * @return false
     */
    public static function insertWkData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(wk_info,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    /**
     * Desc:
     * User:wp
     * Email:1565602678@qq.com
     * Time: 2022/5/20 4:50 下午
     * @param $where
     * @param $data
     * @return bool
     */
    public static function editWkDataByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(wk_info,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }
    public static function pdoQuery($sql){
        return db_query($sql);
    }
    //浏览量加1
    public static function addPageView($wk_id,$themeid){
        $sql = "UPDATE ".tablename(wk_info)." SET page_view = page_view+1 WHERE id='$wk_id' AND themeid='$themeid'";
        $upwk_info = db_query($sql);
        if (!$upwk_info){
            return false;
        }
        return true;
    }
    public static function addWorkerPageView($workerInfo,$themeid){
        $hour = date('Y-m-d H').':00:00';
        $select_sql = " select * from ".tablename(t_workerreport)." WHERE themeid='$themeid' AND workerid=".$workerInfo['uid']." AND uniacid=".$workerInfo['uniacid']." AND hour='$hour' ";
        $maxInfo = db_find($select_sql);
        if(!empty($maxInfo)){
            $sql = " UPDATE ".tablename(t_workerreport)." SET scansum = scansum+1 WHERE id=".$maxInfo['id'];
            $up_info = db_query($sql);
            if (!$up_info){
                return false;
            }
            return true;
        }else{
            //新增
            $insertData = [
                'uniacid'=>$workerInfo['uniacid'],
                'themeid'=>$themeid,
                'workerid'=>$workerInfo['uid'],
                'riqi'=>date('Y-m-d'),
                'renewtime'=>time(),
                'scansum'=>1,
                'hour'=>$hour,
            ];
            $res = $insertid = db_insert(t_workerreport,$insertData);
            if (!empty($res)) {
                return $insertid;
            } else {
                return false;
            }
        }
    }
    //好友达标数量加1
    public static function addFriendNum($wk_id,$themeid,$wk_config){
        $sql = "UPDATE ".tablename(wk_info)." SET friend_num = friend_num+1,config = '$wk_config' WHERE id='$wk_id' AND themeid='$themeid'";
        $upwk_info = db_query($sql);
        if (!$upwk_info){
            return false;
        }
        return true;
    }

    /**
     * Desc:
     * User:wp
     * Email:1565602678@qq.com
     * Time: 2022/5/20 4:51 下午
     * @param $themeid
     * @param bool $one
     * @return mixed
     */
    public static function getCallGiftGroups($themeid,$one = true,$status_limit = true)
    {
        $where = " where themeid='$themeid' ";
        if($status_limit){
            $where .= " and status = 1 ";
        }
        if ($one){
            return db_find(" select id,title,status from "
                . tablename(wk_call_gift_group) . $where ." order by updated_at desc");
        }else{
            return db_select(" select id,title,status from "
                . tablename(wk_call_gift_group) . $where ." order by updated_at desc");
        }
    }

    /**
     * Desc:
     * User:wp
     * Email:1565602678@qq.com
     * Time: 2022/5/20 4:51 下午
     * @param $themeid
     * @param bool $one
     * @return mixed
     */
    public static function getGiftGroups($themeid,$one = true,$status_limit = true)
    {
        $where = " where themeid='$themeid' ";
        if($status_limit){
            $where .= " and status = 1 ";
        }
        if ($one){
            return db_find(" select id,title,status from "
                . tablename(wk_gift_group) . $where ." order by updated_at desc");
        }else{
            return db_select(" select id,title,status from "
                . tablename(wk_gift_group) . $where ." order by updated_at desc");
        }

    }

    public static function getCallGroupInfoById($id,$themeid){
        return db_find("select * from ". tablename(wk_call_gift_group) ." where id='$id' and themeid='$themeid'");
    }

    /**
     * Desc:
     * User:wp
     * Email:1565602678@qq.com
     * Time: 2022/5/22 10:08 上午
     * @param $data
     * @return false
     */
    public static function insertCallGroupData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(wk_call_gift_group,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    /**
     * Desc:
     * User:wp
     * Email:1565602678@qq.com
     * Time: 2022/5/22 10:08 上午
     * @param $where
     * @param $data
     * @return bool
     */
    public static function editCallGroupDataByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(wk_call_gift_group,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    public static function getCallGiftList($call_group_id,$status_limit = true){
        $where = " where group_id='$call_group_id' ";
        if($status_limit){
            $where .= " and status = 1 ";
        }
        return db_select(" select id,title,content,num_limit,status from "
            . tablename(wk_call_gift) . $where ." order by num_limit asc");
    }

    public static function getCallGiftInfoById($call_group_id,$id){
        return db_find("select * from ". tablename(wk_call_gift) ." where id='$id' and group_id='$call_group_id'");
    }

    public static function getCallGiftListByWhere($call_group_id,$num,$status_limit = true){
        $where = " where group_id='$call_group_id' and num_limit <= '$num' ";
        if($status_limit){
            $where .= " and status = 1 ";
        }
        return db_select(" select id,title,content,num_limit,status from "
            . tablename(wk_call_gift) . $where ." order by num_limit asc");
    }

    public static function getCallGiftInfoByWhere($id){
        return db_find("select * from ". tablename(wk_call_gift) ." where id='$id' ");
    }

    public static function insertCallGiftData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(wk_call_gift,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    public static function editCallGiftDataByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(wk_call_gift,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    public static function getGiftGroupInfoById($id,$themeid){
        return db_find("select * from ". tablename(wk_gift_group) ." where id='$id' and themeid='$themeid'");
    }

    public static function insertGiftGroupData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(wk_gift_group,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    public static function editGiftGroupDataByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(wk_gift_group,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    public static function getGiftList($group_id,$status_limit = true){
        $where = " where group_id='$group_id' ";
        if($status_limit){
            $where .= " and status = 1 ";
        }
        return db_select(" select id,title,price,icon,status from "
            . tablename(wk_gift) . $where ." order by price asc");
    }

    public static function getGiftInfoById($group_id,$id){
        return db_find("select * from ". tablename(wk_gift) ." where id='$id' and group_id='$group_id'");
    }

    public static function insertGiftData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(wk_gift,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    public static function editGiftDataByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(wk_gift,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    public static function getCallRecordList($themeid, $wk_id = 0, $search = '', $page_start = 0, $page_size = 10, $workerid = 0)
    {
        $condition = " where a.themeid='$themeid'  ";
        if($workerid > 0){
            $condition .= " AND c.config->'$.workerid' = '$workerid' ";
        }
        if ($search){
            $condition .= " AND (b.nickname LIKE '" . $search . "'". " or c.name LIKE '" . $search . "')" ;
        }
        if ($wk_id){
            $condition .= " AND a.wk_id='$wk_id' " ;
        }
        if($page_size == 0){
            $sql = " select count(distinct a.id) as count_num from "
                . tablename(wk_give_call_record) . " as a " .
                "left join ". tablename(t_fans) . " as b on a.from_uid=b.uid AND b.themeid='$themeid' " .
                "left join ". tablename(wk_info) . " as c on a.wk_id=c.id " .
                $condition;
            return db_find($sql);
        }else{
            $condition .= " order by a.created_at desc ";
            $condition .= "  limit " . $page_start . "," . $page_size;

            $sql = " select a.*,b.avatar,b.nickname,b.mobile,c.name,d.avatar as wk_avatar from "
                . tablename(wk_give_call_record) . " as a " .
                "left join ". tablename(t_fans) . " as b on a.from_uid=b.uid AND b.themeid='$themeid' " .
                "left join ". tablename(wk_info) . " as c on a.wk_id=c.id " .
                "left join ". tablename(t_fans) . " as d on c.uid=d.uid AND d.themeid='$themeid' " .
                $condition;
            return db_select($sql);
        }
    }

    public static function getArecordList($themeid,$ids)
    {
        $condition = " where a.themeid ='$themeid' AND a.status=1 AND a.id in (".$ids.") ";
        $sql = " select a.id,a.uid,b.avatar,b.nickname,b.mobile from "
            . tablename(t_record) . " as a " .
            "left join ". tablename(t_fans) . " as b on a.uid=b.uid  AND b.themeid='$themeid' "
            . $condition;
        return db_select($sql);

    }
    public static function getArecordById($id)
    {
        $sql = " select * from "
            . tablename(t_record) . " where id = '$id' " ;
        return db_select($sql);

    }

    public static function getArecordByUid($themeid,$uid)
    {
        $condition = " where a.themeid='$themeid' AND a.uid='$uid' AND a.status=1";
        $sql = " select a.id from "
            . tablename(t_record) . " as a "
            . $condition;
        return db_select($sql);

    }

    public static function getGiveGiftList($themeid,$wk_id, $ispay = 1){
        $condition = " where a.themeid ='$themeid' AND a.wk_id ='$wk_id' ";
        if ($ispay){
            $condition .= " AND a.status in (1,2) ";
        }
        $sql = " select a.*,b.avatar,b.nickname,c.title,c.icon from "
            . tablename(wk_give_gift_record) . " as a "
            ." left join ". tablename(t_fans) . " as b on a.from_uid=b.uid AND b.themeid='$themeid' "
            ." left join ". tablename(wk_gift) . " as c on a.gift_id=c.id "
            . $condition;
        return db_select($sql);
    }

    public static function getCallRecords($themeid,$wk_id,$uid=0,$start='',$end=''){
        $where = " where themeid='$themeid' and wk_id='$wk_id' ";
        if ($uid){
            $where .= " and from_uid='$uid' ";
        }
        if ($start && $end){
            $where .= " and created_at>='".$start."' and created_at<='".$end ."'";
        }
        return db_select("select * from ".tablename(wk_give_call_record) .$where);
    }

    public static function insertCallRecordData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(wk_give_call_record,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }
    public static function insertGiftRecordData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(wk_give_gift_record,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    public static function getFansInfoByWhere($where)
    {
        return db_get(t_fans, $where);
    }

    public static function giveGiftRecord($config,$orderid,$themeid){

        //查找order信息
        $order = db_find("select * from ". tablename(t_order) ." where id='$orderid'");
        if (empty($order)){
            return false;
        }
        //送礼物
        $wk_id = $order['masterid'];
        $record_id = $order['detailid'];
//        $uid = $order['uid'];
//        $wkInfo = self::getWkInfoById($wk_id,$themeid);
//        $wk_config = json_decode($wkInfo['config'],true);
//        $wk_gift_group_id = $wk_config['wk_gift_group_id'];
        $recordInfo = db_find("select * from ". tablename(wk_give_gift_record) ." where id='$record_id' and wk_id='$wk_id'");
        if (empty($recordInfo)){
            return false;
        }
        if ($recordInfo['status'] != 0){
            return false;
        }
        //修改信息
        $data = [
            'updated_at'=>date("Y-m-d H:i:s"),
            'pay_at'=>date("Y-m-d H:i:s",$order['paytime']),
            'status'=>1,
            'pay_info'=>json_encode(array('order_id'=>$orderid)),
            ];
        $res = db_update(wk_give_gift_record,$data,['id'=>$record_id,'wk_id'=>$wk_id]);
        if (!empty($res)) {
            $free = floatval($order['orderfee']);
            //执行下一步
            //判断打赏配置存入t_common表,打赏的钱到哪 0客户 1影楼
            if ($config['wk_gift_money_give'] == 0){
                $new_fans = self::getFansInfoByWhere(['themeid' => $themeid, 'uid' => $recordInfo['to_uid']]);
                $ordersn = getMillisecond();
                $data = array(
                    'uniacid'    => $new_fans['uniacid'],
                    'themeid'    => $themeid,
                    'recordid'   => $record_id,
                    'ordersn'    => $ordersn,
                    'type'       => 100,
                    'openid'     => $new_fans['openid'],
                    'uid'        => $new_fans['uid'],
                    'addtime'    => TIMESTAMP,
                    'fee'        => $free,
                    'fopenid'    => $new_fans['fopenid'],
                    'fromuid'    => $recordInfo['from_uid'],
                    'openid_old' => $new_fans['openid_old'],
                    'uid_old'    => $new_fans['uid_old'],
                    'fopenid_old'=> $new_fans['fopenid_old'],
                    'fromuid_old'=> $new_fans['fromuid_old'],
                    'status'     =>0,
                );
                //提现
                $comm_id = Comm::add_share_comm($data);
                if (!$comm_id){
                }
            }
            //礼物数+1，+金额
            $sql = "UPDATE ".tablename(wk_info)." SET gift_num = gift_num+1,money = money+".$free." WHERE id='$wk_id' AND themeid='$themeid'";
            $upwk_info = db_query($sql);
            if (!$upwk_info){
            }
            return true;
        } else {
            return false;
        }
    }

    public static function updateWkConfigRecordData($themeid,$recordid,$wk_id){
        $wkInfo = self::getWkInfoById($wk_id,$themeid);
        if (empty($wkInfo)){
            return false;
        }
        $wk_config = $wkInfo['config'];
        $record_ids = $wk_config['arecord_ids'];
        if(empty($record_ids)){
            $wk_config['arecord_ids'] = [$recordid];
        }else{
            if (!in_array($recordid,$record_ids)){
                array_push($record_ids,$recordid);
                $wk_config['arecord_ids'] = $record_ids;
            }else{
                return true;
            }
        }
        $data['config'] = json_encode($wk_config);
        $where = ['id'=>$wk_id];
        //修改数据库字段
        $res = self::editWkDataByWhere($where,$data);
        if (!$res){
            return true;
        }else{
            return false;
        }
    }

    public static function getGiftRecordList($themeid, $wk_id = 0, $search = '', $page_start = 0, $page_size = 10, $workerid = 0)
    {
        $condition = " where a.themeid='$themeid' ";
        if($workerid > 0){
            $condition .= " AND c.config->'$.workerid' = '$workerid' ";
        }
        if ($search){
            $condition .= " AND (b.nickname LIKE '" . $search . "'". " or c.name LIKE '" . $search . "')" ;
        }
        if ($wk_id){
            $condition .= " AND a.wk_id='$wk_id' " ;
        }
        if($page_size == 0){
            $sql = " select count(distinct a.id) as count_num from "
                . tablename(wk_give_gift_record) . " as a " .
                "left join ". tablename(t_fans) . " as b on a.from_uid=b.uid AND b.themeid='$themeid' " .
                "left join ". tablename(wk_info) . " as c on a.wk_id=c.id " .
                $condition;
            return db_find($sql);
        }else{
            $condition .= " order by a.created_at desc ";
            $condition .= "  limit " . $page_start . "," . $page_size;

            $sql = " select a.*,b.avatar,b.nickname,b.mobile,c.name,d.avatar as wk_avatar,e.title as gift_name from "
                . tablename(wk_give_gift_record) . " as a " .
                "left join ". tablename(t_fans) . " as b on a.from_uid=b.uid AND b.themeid='$themeid' " .
                "left join ". tablename(wk_info) . " as c on a.wk_id=c.id " .
                "left join ". tablename(t_fans) . " as d on c.uid=d.uid AND d.themeid='$themeid' " .
                "left join ". tablename(wk_gift) . " as e on a.gift_id=e.id " .
                $condition;
            return db_select($sql);
        }
    }

    public static function getWeikeFriendLimitNum($themeid,$limit_num){
        $sql = " select count(a.id) as count_num from "
            . tablename(wk_info) . " as a".
            " where a.themeid='$themeid' AND a.delete = 0 AND a.friend_num >= '$limit_num'";
        $res = db_find($sql);
        return $res['count_num'];
    }

    public static function getMusicList($themeid, $status_limit = true, $is_all = true, $search = '', $page_start = 0, $page_size = 10)
    {
        $where = " where themeid='$themeid' ";
        if($status_limit){
            $where .= " and status = 1 ";
        }
        if ($search){
            $where .= " AND name LIKE '" . $search . "'" ;
        }
        if($is_all){
            return db_select(" select id,name,url,status from "
                . tablename(wk_music) . $where ." order by updated_at desc");
        }
        if($page_size == 0){
            $sql = " select count(id) as count_num from "
                . tablename(wk_music) . $where ;
            return db_find($sql);
        }else{
            $where .= " order by created_at desc ";
            $where .= "  limit " . $page_start . "," . $page_size;

            $sql = " select * from "
                . tablename(wk_music) . $where ;
//            var_dump($sql);die;
            return db_select($sql);
        }
    }
    public static function getMusicInfoById($id,$themeid){
        $result = db_find("select * from ". tablename(wk_music) ." where id='$id' and themeid='$themeid'");
        return $result;
    }
    public static function insertMusicData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(wk_music,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }
    public static function editMusicDataByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(wk_music,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    public static function addArecord($data){
        $r = $insertid = db_insert(t_record,$data);
        if(!empty($r)){
            return $insertid;
        }else{
            return false;
        }
    }
    public static function editArecordByWhere($where,$data)
    {
        $res = db_update(t_record,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    public static function getWorkerWeikeInfo($themeid, $config, $workerid = 0){
        //客片
        $wk_where = '';
        if($workerid){
            $wk_where = " AND a.config->'$.workerid' = '$workerid' ";
        }
        $wk_count = self::getWeikeCountByWhere($themeid,$wk_where);
        //客资
        $where = " where themeid='$themeid' ";
        if($workerid > 0){
            $where .= " AND workerid = '$workerid' ";
        }
        $fansData = db_find(" select count(id) as count_num from " . tablename(t_fans_other_form) . $where);
        $fansinfo_count = $fansData['count_num'];

        //订单
        $orderData = db_find(" select count(id) as count_num from " . tablename(t_record) . $where);
        $order_count = $orderData['count_num'];

        //昨日浏览  昨日打赏/昨日转发 昨日打CALL
        $yesterday = date('Y-m-d',strtotime('-1 day'));
        $yesterdayData = db_find(" select sum(scansum) as view_count,sum(forward) as share_count from " . tablename(t_workerreport) . $where ." AND riqi='$yesterday' ");
        $yesterday_view = $yesterdayData['view_count']?:0;
        $yesterday_share = $yesterdayData['share_count']?:0;
        $record_where = " where a.themeid='$themeid' ";
        if($workerid > 0){
            $record_where .= " AND c.config->'$.workerid' = '$workerid' ";
        }
        $yesterday_start = $yesterday." 00:00:00";
        $yesterday_end = $yesterday." 23:59:59";
        $yesterday_where = " AND a.created_at >='$yesterday_start' AND a.created_at <='$yesterday_end' ";

        $gift_sql = " select sum(a.price) as money_count from "
            . tablename(wk_give_gift_record) . " as a " .
            "left join ". tablename(wk_info) . " as c on a.wk_id=c.id " ;
        $yesterdayMoney =  db_find($gift_sql . $record_where .$yesterday_where. " AND a.status=1 ");
        $yesterday_money = $yesterdayMoney['money_count']?:0;

        $call_sql = " select count(distinct a.id) as count_num from "
            . tablename(wk_give_call_record) . " as a " .
            "left join ". tablename(wk_info) . " as c on a.wk_id=c.id " ;
        $yesterdayCall = db_find($call_sql. $record_where .$yesterday_where);
        $yesterday_call = $yesterdayCall['count_num'];
        //今日浏览  今日打赏/今日转发 今日打CALL
        $today = date('Y-m-d');
        $todayData = db_find(" select sum(scansum) as view_count,sum(forward) as share_count from " . tablename(t_workerreport) . $where ." AND riqi='$today' ");
        $today_view = $todayData['view_count']?:0;
        $today_share = $todayData['share_count']?:0;
        $today_start = $today." 00:00:00";
        $today_end = $today." 23:59:59";
        $today_where = " AND a.created_at >='$today_start' AND a.created_at <='$today_end' ";
        $todayMoney =  db_find($gift_sql . $record_where .$today_where. " AND a.status=1 ");
        $today_money = $todayMoney['money_count']?:0;
        $todayCall = db_find($call_sql. $record_where .$today_where);
        $today_call = $todayCall['count_num'];
//        $data = [
//            'wk_count'=>intval($wk_count),
//            'fansinfo_count'=>intval($fansinfo_count),
//            'order_count'=>intval($order_count),
//            'yesterday_view'=>intval($yesterday_view),
//            'yesterday_share'=>intval($yesterday_share),
//            'yesterday_money'=>floatval($yesterday_money),
//            'yesterday_call'=>intval($yesterday_call),
//            'today_view'=>intval($today_view),
//            'today_share'=>intval($today_share),
//            'today_money'=>floatval($today_money),
//            'today_call'=>intval($today_call),
//        ];
        $top_data = [
            ['name'=>'客片','num'=>intval($wk_count)],
            ['name'=>'客资','num'=>intval($fansinfo_count)],
            ['name'=>'订单','num'=>intval($order_count)],
            ['name'=>'昨日浏览','num'=>intval($yesterday_view)],
        ];
        if($config['wk_gift_open']){
            array_push($top_data,['name'=>'昨日打赏','num'=>floatval($yesterday_money)]);
        }else{
            array_push($top_data,['name'=>'昨日转发','num'=>intval($yesterday_share)]);
        }
        if($config['wk_call_open']){
            array_push($top_data,['name'=>'昨日打call','num'=>intval($yesterday_call)]);
        }else{
            $yesterday_fansinfo = db_find(" select count(id) as count_num from " . tablename(t_fans_other_form) . $where . " AND created_at >='$yesterday_start' AND created_at <='$yesterday_end' ");
            array_push($top_data,['name'=>'昨日客资','num'=>intval($yesterday_fansinfo['count_num'])]);
        }
        array_push($top_data,['name'=>'今日浏览','num'=>intval($today_view)]);
        if($config['wk_gift_open']){
            array_push($top_data,['name'=>'今日打赏','num'=>floatval($today_money)]);
        }else{
            array_push($top_data,['name'=>'今日转发','num'=>intval($today_share)]);
        }
        if($config['wk_call_open']){
            array_push($top_data,['name'=>'今日打call','num'=>intval($today_call)]);
        }else{
//            var_dump(" select count(id) as count_num from " . tablename(t_fans_other_form) . $where . " AND created_at >='$today_start' AND created_at <='$today_end' ");
            $today_fansinfo = db_find(" select count(id) as count_num from " . tablename(t_fans_other_form) . $where . " AND created_at >='$today_start' AND created_at <='$today_end' ");
            array_push($top_data,['name'=>'今日客资','num'=>intval($today_fansinfo['count_num'])]);
        }
        //处理微客管理模块
        $url_first = [
            ['url'=>'/Worker/pages/w_wklist/w_wklist','open_type'=>'navigate','icon'=>'/Worker/images/icon10.png','name'=>'微客列表'],
            ['url'=>'/Worker/pages/w_order/w_order','open_type'=>'navigate','icon'=>'/Worker/images/icon16.png','name'=>'订单'],
            ['url'=>'/Worker/pages/w_other_form/w_other_form','open_type'=>'navigate','icon'=>'/Worker/images/icon12.png','name'=>'表单收集'],
            ['url'=>'/Worker/pages/w_wkadd/w_wkadd','open_type'=>'navigate','icon'=>'/Worker/images/plus.png','name'=>'发布客片'],
        ];
        $url_secend = [
            ['url'=>'/Worker/pages/w_wkcall/w_wkcall?op=call','open_type'=>'navigate','icon'=>'/Worker/images/icon16.png','name'=>'打call记录'],
            ['url'=>'/Worker/pages/w_wkcall/w_wkcall?op=gift','open_type'=>'navigate','icon'=>'/Worker/images/icon11.png','name'=>'打赏记录'],
        ];
        return ['top_data'=>$top_data,'url_first'=>$url_first,'url_secend'=>$url_secend];
    }

    public static function getWkInfoByOtherformUid($themeid,$uid){
        return db_find(" SELECT id,name from ".tablename(wk_info)." WHERE themeid='$themeid' AND (JSON_CONTAINS(config->'$.collect_ids', JSON_ARRAY('$uid')) OR JSON_CONTAINS(config->'$.gift_collect_ids', JSON_ARRAY('$uid'))) ");
    }

    public static function getTemplateInfo($system,$id=0){
        $softsndata = array(
            'id'    => $id,
        );
        $info = Poster::Getbanklist($system,'get_weike_template',$softsndata);
        if (!empty($id)){
            return $info[0];
        }
        return $info;
    }

    public static function editWkRecordInfo($themeid,$config){
        //已绑定，没有虚拟订单的客片
        $wk_sql = " select a.*,b.uniacid,b.openid,b.nickname,b.avatar,b.username,b.mobile from "
            . tablename(wk_info) . " as a " .
            "left join ". tablename(t_fans) . " as b on a.uid=b.uid AND b.themeid='$themeid' " .
            " where a.themeid='$themeid'  AND a.delete = 0 AND a.uid > 0 AND recordid = 0";
        $wk_list = db_select($wk_sql);
        if (!empty($wk_list)){
            foreach ($wk_list as $v){
                $fee = floatval($config['groupprice']);
                $uncode = $themeid.'-'.TIMESTAMP.rand(1000,1000000);
                $wk_config = json_decode($v['config'],true);
                //如果没有recordid，进行新增;如果有进行修改
                $record_data = [
                    'uniacid'  => $v['uniacid'],
                    'themeid'  => $themeid,
                    'openid'   => $v['openid'],
                    'uid'      => $v['uid'],
                    'nickname' => $v['nickname'],
                    'avatar'   => $v['avatar'] ,
                    'username' => $v['username'],
                    'mobile'   => $v['mobile'],
                    'status'   => -2,//虚拟定单
                    'fromuid'  => $v['uid'],
                    'fopenid'  => $v['openid'],
                    'workerid'  => intval($wk_config['workerid']),
                ];
                $record_data['addtime'] = TIMESTAMP;
                $record_data['fee'] = $fee;
                $record_data['uncode'] = $uncode;
                $record_data['type'] = 0;
                $reordid = WeikeModel::addArecord($record_data);
                //更新pid
                if ($reordid){
                    WeikeModel::editArecordByWhere(['id'=>$reordid],['pid'=>$reordid]);
                    WeikeModel::editWkDataByWhere(['id'=>$v['id']],['recordid'=>$reordid]);
                }
            }
        }
    }
}